//
//  NSAttributedString+Category.m
//  PKWSevers
//
//  Created by Seven on 16/5/23.
//  Copyright © 2016年 Seven. All rights reserved.
//

#import "NSAttributedString+Category.h"
#import <CoreText/CoreText.h>
@implementation NSAttributedString (Category)
- (CGFloat)heightWithContainWidth:(CGFloat)width{
    int total_height = 0;
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self);
    CGRect drawingRect = CGRectMake(0, 0, width, 100000);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, drawingRect);
    CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path, NULL);
    CGPathRelease(path);
    CFRelease(framesetter);
    
    NSArray *linesArray = (NSArray *) CTFrameGetLines(textFrame);
    if(linesArray.count == 0)return 0;
    CGPoint origins[[linesArray count]];
    CTFrameGetLineOrigins(textFrame, CFRangeMake(0, 0), origins);
    int line_y = (int) origins[[linesArray count] -1].y;
    CGFloat ascent;
    CGFloat descent;
    CGFloat leading;
    CTLineRef line = (__bridge CTLineRef) [linesArray objectAtIndex:[linesArray count]-1];
    CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
    
    total_height = 100000 - line_y + (int) descent +1;
    CFRelease(textFrame);
    return total_height;
}


+(NSMutableAttributedString *)generateAttriuteStringWithStr:(NSString *)str WithColor:(UIColor *)color WithFont:(UIFont *)font
{
    if (str == nil) {
        return nil;
    }
    
    if (font == nil) {
        font = [UIFont boldSystemFontOfSize:16.0f];
    }
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:str];
    NSRange range;
    range.location = 0;
    range.length = attrString.length;
    [attrString beginEditing];
    [attrString addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color,NSForegroundColorAttributeName,font,NSFontAttributeName, nil] range:range];
    //左右两端对齐
    //attrString.alignment = NSTextAlignmentJustified;
    //[attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleNone] range:NSMakeRange(0, [str length])];
    [attrString endEditing];
    return attrString;
}

/**生成htmlAttriuteString*/
+ (NSMutableAttributedString *)generateAttriuteStringWithHtml:(NSString *)htmlString{
    NSError *error = nil;
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil error:&error];
    if (error) {
        return nil;
    }
    return string;
}

- (CGRect)rectWithWidth:(CGFloat)width{
    return [self rectWithSize:CGSizeMake(width, CGFLOAT_MAX)];
}

- (CGRect)rectWithHeight:(CGFloat)height{
    return [self rectWithSize:CGSizeMake(height, CGFLOAT_MAX)];
}

- (CGRect)rectWithSize:(CGSize)size{
    CGRect rect = [self boundingRectWithSize:size options:NSStringDrawingDefault context:nil];
    rect.size.width = ceil(MIN(rect.size.width, size.width));
    rect.size.height = ceil(MIN(rect.size.height, size.height));
    return rect;
}

@end


@implementation NSMutableAttributedString (Shadow)

- (void)setShadow{
    [self setShadowWithColor:ColorWithHexAplha(0x121212, 0.7)];
}

- (void)setShadowWithColor:(UIColor *)color{
    
    NSShadow *shadow = [[NSShadow alloc]init];
    shadow.shadowOffset = CGSizeMake(0, 2);
    shadow.shadowColor = color;
    shadow.shadowBlurRadius = 1;
    self.shadow = shadow;
}

@end
